package com.example.springboot.ytest.ytest;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;

import com.alibaba.fastjson.JSON;

class CloneObj implements Cloneable{
	
	public CloneObj(String s, long l) {
		super();
		this.s = s;
		this.l = l;
	}
	@Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
	private String s;
	private long l;
	public String getS() {
		return s;
	}
	public void setS(String s) {
		this.s = s;
	}
	public long getL() {
		return l;
	}
	public void setL(long l) {
		this.l = l;
	}
}

class User{
	public User(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
}

/**
 * 深拷贝测试
 * 
 * @author: yeyi
 * @date: 2019年7月2日
 */
public class DeepCopy {
	
	static void testUser() {
		User u1 = new User("路飞", 17);
		User u2 = new User("", 0);
		try {
			BeanUtils.copyProperties(u2, u1);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		System.out.println(u1==u2);
		System.out.println(JSON.toJSONString(u1));
		System.out.println(JSON.toJSONString(u2));
	}
	
	static void testClone() {
		CloneObj obj = new CloneObj("你好", 10L);
		try {
			CloneObj obj2 = (CloneObj) obj.clone();
			System.out.println(obj==obj2);
			System.out.println(JSON.toJSONString(obj));
			System.out.println(JSON.toJSONString(obj2));
			obj2.setL(1);
			System.out.println(JSON.toJSONString(obj));
			System.out.println(JSON.toJSONString(obj2));
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
	
    public static void main(String[] args) {
        testUser();
//    	testClone();
    }
}
